#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   curd.py
@Time    :   2021/02/08 00:12:14
@Author  :   superjin
@Version :   1.0
'''

from hashlib import new
from os import name
from typing import Any, Optional, List
from sqlalchemy import func,and_
from sqlalchemy.orm import Session
from core.schemas.participant import ParticipantCreate, ParticipantDelete
from models.user import User
from models.participant import Participant
from models.classroom import Classroom
from models.reservation import Reservation
from  models.device import Device


class CRUDDevice:
    def remove(self,db: Session,delete_participant:ParticipantDelete ):
        db.query(Participant).filter_by(Participant.participant_id.in_(
            delete_participant.participant_ids)).delete(synchronize_session=False)
        db.commit()
        return {}
    
    def create(self, db: Session, reservation_id:int,users: []) :
        participants = []
        for i in users:
            participants.append(Participant(reservation_id,i))
        db.add_all(participants)
        db.commit()
        return participants

    def list_all_devices(self, db: Session, page:int ,page_size:int) :
        res = db.query(Device.device_id,
                        Device.classroom_id,
                        Device.device_type,
                        Device.stream_url, 
                        Device.key, 
                        Device.status, 
                        func.concat(Classroom.building_name,'-',Classroom.room_number).label('location')
                        ).join(Classroom,
                    Device.classroom_id == Classroom.classroom_id).offset((page-1)*page_size).limit(page_size).all()
        total = db.query(func.count(Device.device_id)).scalar()
        return res,total


device = CRUDDevice()
